/*
 * @lc app=leetcode.cn id=225 lang=javascript
 *
 * [225] 用队列实现栈
 */

// @lc code=start

class MyQueue {
  constructor() {
    this.items = {};
    this.count = 0;
    this.lowestCount = 0;
  }

  enqueue(element) {
    this.items[this.count++] = element;
  }

  dequeue() {
    const res = this.items[this.lowestCount];
    delete this.items[this.lowestCount++];
    return res;
  }

  peek() {
    return this.items[this.lowestCount];
  }

  size() {
    return this.count - this.lowestCount;
  }

  empty() {
    return this.size() === 0;
  }

  clear() {
    this.items = {};
    this.count = 0;
    this.lowestCount = 0;
  }
}

var MyStack = function () {
  this.q1 = new MyQueue();
  this.q2 = new MyQueue();
};

/**
 * @param {number} x
 * @return {void}
 */
MyStack.prototype.push = function (x) {
  this.q1.enqueue(x);
};

/**
 * @return {number}
 */
MyStack.prototype.pop = function () {
  if (this.q1.empty()) {
    return undefined;
  }

  while(this.q1.size() > 1) {
    this.q2.enqueue(this.q1.dequeue());
  }
  const res = this.q1.dequeue();
  while(this.q2.size()) {
    this.q1.enqueue(this.q2.dequeue());
  }
  return res;
};

/**
 * @return {number}
 */
MyStack.prototype.top = function () {
  const res = this.pop();
  this.q1.enqueue(res);
  return res;
};

/**
 * @return {boolean}
 */
MyStack.prototype.empty = function () {
  return this.q1.empty();
};

/**
 * Your MyStack object will be instantiated and called as such:
 * var obj = new MyStack()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.empty()
 */
// @lc code=end
